package cm.unit4.theme3;

import cm.commom.TreeNode;

public class HasPathSumDemo {
    public boolean hasPathSum (TreeNode root, int sum) {
        if (root==null || (root.val==sum && root.right==null && root.left!=null)
                || (root.val==sum && root.right!=null && root.left==null)){
            return false;
        }
        return hasPathSumHelper(root,sum);

    }

    public boolean hasPathSumHelper (TreeNode root, int sum){
        if (root==null){
            return sum == 0;
        }else{
            return (hasPathSumHelper(root.left,sum-root.val)
                    || hasPathSumHelper(root.right,sum-root.val));
        }
    }

    public static void main(String[] args) {
        HasPathSumDemo obj = new HasPathSumDemo();
        TreeNode root = TreeNode.deserialize("1!2!#");
        System.out.println(TreeNode.prePrint(root));
        boolean result = obj.hasPathSum(root, 1);
        System.out.println(result);
    }
}
